class TextMeasure {
	fontSize = 16
	fontFamily = ''
	letterSpacing = '0'
	styles = {
		'line-height': '1',
		'color': 'black',
		'position': 'absolute',
		'top': '0px',
		'left': '0px',
		'background-color': 'lightcoral'
		// 'z-index': '-1',
		// 'visibility': 'hidden'
	}
	
	id = '__text_measure__'
	constructor () {
		// if (options) {
		// 	Object.assign(this.styles, options)
		// }
		let div = document.createElement('div')
		Object.getOwnPropertyNames(this.styles).forEach(name => {
			div.style[name] = this.styles[name]
		})
		div.style.fontSize = this.fontSize + 'px'
        if (this.fontFamily) {
            div.style.fontFamily = this.fontFamily
        }
		div.style.letterSpacing = this.letterSpacing + 'px'
		div.setAttribute('id', this.id)
		document.body.append(div)
	}
	
	setWidth (value) {
		document.getElementById(this.id).style.width = value + 'px'
	}
	setFontSize (value) {
		this.fontSize = value
		document.getElementById(this.id).style.fontSize = value + 'px'
	}
	setFontFamily (value) {
		this.fontFamily = value
		document.getElementById(this.id).style.fontFamily = value
	}
	setLetterSpacing (value) {
		this.letterSpacing = value
		document.getElementById(this.id).style.letterSpacing = value + 'px'
	}
	
	writeText(content) {
		document.getElementById(this.id).innerText = content
	}
	
	/**
	 * 测试传入的文字内容是否超出一行，可设置是否缩进（默认两个字宽）
	 * @param {String} content 文字内容
	 * @param {Boolean} textIndent 是否缩进
	 * @return true|false
	 */
	testOutLine(content, textIndent = false) {
		let ele = document.getElementById(this.id)
		if (textIndent) {
			ele.style.textIndent = this.fontSize * 2 + 'px'
		} else {
			ele.style.textIndent = '0px'
		}
		ele.innerText = content
		return ele.clientHeight > this.fontSize
	}
}

export default TextMeasure
